/*
 * Created on 2007/02/21
 * CopyRight by jtbbs.
 */
package com.atjava.jcrt.utility;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 
 * PropertyHelper
 * 
 * @author biggie
 * @version $Revision: 1.1 $ $Date: 2008/08/07 00:52:23 $
 * 
 */
public class PropertyHelper {
	
    /**
     * Log4j Logger instance.
     */
    private Logger log = Logger.getLogger(PropertyHelper.class.getName());

	public static final String SYSTEM_CONFIGFILE="sysconf.properties";

	private String configFile = null;

	private Properties properties = null;

	private static Map propertiesMap = new HashMap();
	
	public PropertyHelper(){
		this.configFile = "/"+SYSTEM_CONFIGFILE;
		init(this.getClass());
	}

	public PropertyHelper(String propertiesFile){
		this.configFile = "/"+propertiesFile;
		init(this.getClass());
	}

	public PropertyHelper(Class clazz,String propertiesFile){
		this.configFile = propertiesFile;
		init(clazz);
	}

	private void init(Class clazz){
		if(propertiesMap.get(this.configFile)!=null){
			this.properties = (Properties)propertiesMap.get(this.configFile);
		}else{
			load(clazz);
		}
	}

	private void load(Class clazz){
		properties = new Properties();
		InputStream input = null;
		try{
			
			input = clazz.getResourceAsStream(this.configFile);
			properties.load(input);
		}catch(Exception e){
			log.error("Load properties file ["+this.configFile+"] is faild..."+e);
		}finally{
			try{
				if(input != null){
					input.close();
				}
			}catch(Exception e){
				log.error("Can not close the InputStream..."+e);
			}
		}
	}

	public Properties getProperties(){
		return this.properties;
	}

	public String getValue(String key){
		String str = null;
		try{
			str = this.properties.getProperty(key);
		}catch(Exception e){
			log.error(e);
		}
		return str;
	}

	public String getPropertiesFile(){
		return this.configFile;
	}
	
}
